package com.tanhua.bg.service;

import cn.hutool.core.date.DateUtil;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.bg.mapper.UserInfoMapper;


import com.tanhua.bg.pojo.UserCheck;
import com.tanhua.bg.pojo.UserInfo;
import com.tanhua.bg.pojo.Video;
import com.tanhua.bg.vo.PageResult;
import com.tanhua.bg.vo.VideoCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class VideoCheckService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private UserInfoMapper userInfoMapper;

    public PageResult queryVideoList(Integer page, Integer pageSize, String uid, String sortProp, String sortOrder) {
        //构建一个新的分页参数
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);
        VideoCheck videoCheck = new VideoCheck();
        PageRequest pageRequest = null;
//        做排序,按不同条件
        if (sortOrder == "ascending") {
            if (sortProp == "createDate") {
                pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.asc("created")));
            } else if (sortProp == "reportCount") {
                pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.asc("reportCount")));
            } else if (sortProp == "forwardingCount") {
                pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.asc("forwardingCount")));
            } else if (sortProp == "commentCount") {
                pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.asc("commentCount")));
            } else {
                pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.asc("likeCount")));
            }
        } else {
            if (sortProp == "createDate") {
                pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("created")));
            } else if (sortProp == "reportCount") {
                pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("reportCount")));
            } else if (sortProp == "forwardingCount") {
                pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("forwardingCount")));
            } else if (sortProp == "commentCount") {
                pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("commentCount")));
            } else {
                pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("likeCount")));
            }
        }


        //开始查询数据
//        PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.asc("created")));
        //构建查询对象
        Query query = new Query(Criteria.where("userId").is(Long.valueOf(uid))).with(pageRequest);
        //查询数据
        List<Video> videos = this.mongoTemplate.find(query, Video.class);

        List<VideoCheck> videoCheckList = new ArrayList<>();
        //循环遍历出苏需要的值
        for (Video video : videos) {
            videoCheck.setId(video.getId());
            videoCheck.setCreateDate(DateUtil.format(new Date(video.getCreated()), "yyyy-MM-dd HH:mm"));
            videoCheck.setUserId(video.getUserId());
            videoCheck.setPicUrl(video.getPicUrl());
            videoCheck.setVideoUrl(video.getVideoUrl());
            videoCheckList.add(videoCheck);
        }

        Query queryUser = new Query(Criteria.where("userid").is(uid));
        List<UserCheck> userCheckList = this.mongoTemplate.find(queryUser, UserCheck.class);
        for (UserCheck userCheck : userCheckList) {
            //封装数据
            //评论数
            videoCheck.setCommentCount(userCheck.getCommentCount());
            //跟随数
            videoCheck.setForwardingCount(userCheck.getForwardingCount());
            //点赞数
            videoCheck.setLikeCount(userCheck.getLikeCount());
            //评论数
            videoCheck.setReportCount(userCheck.getReportCount());
            videoCheckList.add(videoCheck);
        }

        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", uid);
        UserInfo userInfo = this.userInfoMapper.selectOne(wrapper);
        videoCheck.setNickname(userInfo.getNickName());

        pageResult.setItems(videoCheckList);
        return pageResult;
    }

}
